// src/router/index.js
import {
	createRouter,
	createWebHistory
} from 'vue-router';

import {
	getToken
} from '@/utils/token.js'

import Home from '@/views/home/Home.vue'; // 导入 Home 布局组件
import Login from '@/views/user/Login.vue';
import UserManager from '@/views/user/UserManager.vue';
import UserDetail from '@/views/user/UserDetail.vue';
import UserRoleEdit from '@/views/user/UserRoleEdit.vue';
import EmployeeRegister from '@/views/user/EmployeeRegister.vue';
import Role from '@/views/user/Role.vue';
import RoleDetail from '@/views/user/RoleDetail.vue';
import RolePermissionEdit from '@/views/user/RolePermissionEdit.vue';
import Permission from '@/views/user/Permission.vue';
import Order from '@/views/order/Order.vue';
import OrderDetail from '@/views/order/OrderDetail.vue';
import OrderEdit from '@/views/order/OrderEdit.vue';
import Refund from '@/views/order/Refund.vue';
import Course from '@/views/course/Course.vue';
import CourseDetail from '@/views/course/CourseDetail.vue';
import CourseEdit from '@/views/course/CourseEdit.vue';
import Room from '@/views/room/Room.vue';
import RoomEdit from '@/views/room/RoomEdit.vue';
import RoomDetail from '@/views/room/RoomDetail.vue';
import Product from '@/views/product/Product.vue';
import ProductEdit from '@/views/product/ProductEdit.vue';
import ProductDetail from '@/views/product/ProductDetail.vue';
import CategoryEdit from '@/views/product/CategoryEdit.vue';
import Categorie from '@/views/product/Categorie.vue';
import CategorieDetail from '@/views/product/CategorieDetail.vue';
import WorkTask from '@/views/work/WorkTask.vue';


const routes = [{
		//重定向到主页 
		path: '/',
		redirect: '/login'
	}, {
		path: '/home',
		component: Home, // 使用 Home 作为布局
		history: createWebHistory(), //启用 history 模式
		children: [
			// 子路由将渲染到 Home 的 <router-view> 中
			{
				path: '', // 空路径代表默认子路由
				name: 'HomeView',
				component: () => import('@/views/home/HomePage.vue'), // 实际首页内容组件
				meta: {
					title: '首页',
					title: 'Promotion Management',
					tab: true, // 标记为独立标签页
					fixed: true // 标记为固定标签不可关闭
				}, // 用于标签上显示的 title // 将路由参数传递给组件
			},

			//--------------------------------------------------------

			{
				path: '/order', // 使用动态参数 :tabName 匹配标签页 name
				component: Order,
				meta: {
					title: '订单页面',
					tab: true, // 标记需要显示在标签页
				},
			},
			{
				path: '/order/:id',
				component: OrderDetail,
				meta: {
					title: '订单详情',
					tab: true,
				},
			},
			{
				path: '/order/edit/:id',
				component: OrderEdit,
				meta: {
					title: '编辑订单',
					tab: true,
				},
			},
			{
				path: '/order/refund',
				component: Refund,
				meta: {
					title: '退款页面',
					tab: true
				},
			},

			//--------------------------------------------------------

			{
				path: '/course',
				component: Course,
				meta: {
					title: '课程列表',
					tab: true
				},
			},
			{
				path: '/course/:id',
				component: CourseDetail,
				meta: {
					title: '课程详情',
					tab: true,
				},
			},
			{
				path: '/course/:mode(edit|add)/:id?',
				name: 'course-edit', // 必须与 editableTabs 中的 name 一致
				component: CourseEdit,
				meta: {
					title: '创建课程',
					tab: true,
					requiresAuth: true
				},
			},

			//--------------------------------------------------------

			{
				path: '/room',
				component: Room,
				meta: {
					title: '包间页面',
					tab: true,
				},
			},
			{
				path: '/room/:id',
				component: RoomDetail,
				meta: {
					title: '包间详情',
					tab: true,
				},
			},
			{
				path: '/room/:mode(edit|add)/:id?',
				name: 'room-edit', // 必须与 editableTabs 中的 name 一致
				component: RoomEdit,
				meta: {
					title: '新增包间',
					tab: true,
					requiresAuth: true
				},
			},

			//--------------------------------------------------------

			{
				path: '/product',
				component: Product,
				meta: {
					title: '商品页面',
					tab: true,
				},
			},
			{
				path: '/product/:id',
				component: ProductDetail,
				meta: {
					title: '商品详情',
					tab: true,
				},
			},
			{
				path: '/product/:mode(edit|add)/:id?',
				name: 'product-edit', // 必须与 editableTabs 中的 name 一致
				component: ProductEdit,
				meta: {
					title: '创建商品',
					tab: true,
					requiresAuth: true
				},
			},
			{
				path: '/category',
				component: Categorie,
				meta: {
					title: '分类管理',
					tab: true,
				},
			},
			{
				path: '/category/:mode(edit|add)/:id?',
				name: 'category-edit', // 必须与 editableTabs 中的 name 一致
				component: CategoryEdit,
				meta: {
					title: '添加商品分类',
					tab: true,
					requiresAuth: true
				},
			},
			{
				path: '/category/:id',
				component: CategorieDetail,
				meta: {
					title: '分类详情',
					tab: true,
				},
			},
			//--------------------------------------------------------

			{
				path: '/work',
				component: WorkTask,
				meta: {
					title: '工作任务管理',
					tab: true,
				},
			},

			//--------------------------------------------------------
			{
				path: '/user/manager',
				component: UserManager,
				meta: {
					title: '用户列表',
					tab: true,
				},
			},
			{
				path: '/user/manager/:id',
				component: UserDetail,
				meta: {
					title: '用户详情',
					tab: true,
				},
			},
			{
				path: '/user/roleUser/edit/:id',
				component: UserRoleEdit,
				meta: {
					title: '编辑用户角色',
					tab: true,
				},
			},
			{
				path: '/user/role',
				component: Role,
				meta: {
					title: '角色列表',
					tab: true,
				},
			},
			{
				path: '/user/roleDetail/:id',
				component: RoleDetail,
				meta: {
					title: '角色详情',
					tab: true,
				},
			},
			{
				path: '/user/rolePermission/edit/:id',
				component: RolePermissionEdit,
				meta: {
					title: '编辑角色权限',
					tab: true,
				},
			},
			{
				path: '/user/permission',
				component: Permission,
				meta: {
					title: '权限列表',
					tab: true,
				},
			},
			{
				path: '/user/employeeRegister',
				component: EmployeeRegister,
				meta: {
					title: '员工注册',
					tab: true,
				},
			},
		],
	},
	{
		path: '/login',
		component: Login,
	},
	{
		// path: '*',新版VueRouter已废弃以‘*’为通配符，404
		path: '/:pathMatch(.*)*',
		name: 'NotFound',
		component: () => import('@/views/NotFind.vue')
	},
	{
		//403
		path: '/unauthorized',
		name: 'Unauthoized',
		component: () => import('@/views/Unauthorized.vue')
	},
];

const router = createRouter({
	history: createWebHistory(),
	routes,
});

/**
 * 动态标题映射表
 */
const courseTitleMap = {
	'edit': '编辑课程',
	'add': '创建课程'
}
const roomTitleMap = {
	'edit': '编辑包间',
	'add': '创建包间'
}
const productTitleMap = {
	'edit': '编辑商品',
	'add': '新增商品'
}
const categoryTitleMap = {
	'edit': '编辑商品分类',
	'add': '添加商品分类'
}
/**
 * 全局路由守卫
 * to：到哪里去，到哪去的完整路由信息对象（路径，参数）
 * from：从哪里来，从哪来的完整路由信息对象（路径，参数）
 * next()：是否放行
 * （1）：next()==直接放行，放行到to的这个路径
 * （2）：next(路径)==进行拦截，拦截到next里面配置的路径
 */
/**
 * 不需要验证，直接放行的页面
 */
const noAuthUrls = ["/login", "/unauthorized"];

router.beforeEach((to, from, next) => {
	//  动态设置页面标题
	if (to.path.startsWith('/course/')) {
		to.meta.title = courseTitleMap[to.params.mode] || to.meta.title
	}
	//  动态设置页面标题
	if (to.path.startsWith('/room/')) {
		to.meta.title = roomTitleMap[to.params.mode] || to.meta.title
	}
	if (to.path.startsWith('/category/')) {
		to.meta.title = categoryTitleMap[to.params.mode] || to.meta.title
	}
	//  动态设置页面标题
	if (to.path.startsWith('/product/')) {
		to.meta.title = productTitleMap[to.params.mode] || to.meta.title
	}

	// 1. 检查是否在无需认证的白名单中
	if (noAuthUrls.includes(to.path)) {
		next() // 放行登录页 
		return
	}

	// 2. 需要认证的路径，检查 token
	const token = getToken()
	if (token) {
		next()
	} else {
		// 未登录时重定向到未授权页
		next('/unauthorized')
	}
})

export default router;